package com.person.yan.a01_retrofit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.person.yan.a01_retrofit.bean.HomeBean;
import com.person.yan.a01_retrofit.bean.ResponseInfo;
import com.person.yan.a01_retrofit.nat.NetInstanseUtils;
import com.person.yan.a01_retrofit.nat.TakeOutApi;
import com.person.yan.a01_retrofit.nat.impl.SimpleCallBack;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView activityMainBtnTv;
    private TextView activity_main_btn_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        activity_main_btn_tv = (TextView) findViewById(R.id.activity_main_btn_tv);


        activity_main_btn_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.activity_main_btn_tv :

                connectNat();

                break;
        }
    }

    private void connectNat() {

        TakeOutApi takeOutApi = NetInstanseUtils.getNatInstanceApi();
        Call<ResponseInfo> api = takeOutApi.getApi();


//        api.enqueue(new Callback<ResponseInfo>() {
//            @Override
//            public void onResponse(Call<ResponseInfo> call, Response<ResponseInfo> response) {
//                ResponseInfo body = response.body();
//                Toast.makeText(MainActivity.this, body.getData(), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onFailure(Call<ResponseInfo> call, Throwable t) {
//                t.printStackTrace();
//            }
//        });


        api.enqueue(new SimpleCallBack() {
            @Override
            public void returnError(int i, Throwable e) {

            }

            @Override
            public void returnData(int i, String json) {
                HomeBean homeBean = new Gson().fromJson(json, HomeBean.class);
                Toast.makeText(MainActivity.this, homeBean.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

